/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jule.clases;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 *
 * @author Administrador
 */
public class fileUtil {

    /**
     * Returns the directory path portion of a file specification string.
     * Matches the equally named unix command.
     * @return The directory portion excluding the ending file separator.
     */
    public static String dirname(String filename) {
        int i = filename.lastIndexOf(File.separator);
        return (i >= 0) ? filename.substring(0, i) : "";
    }

    /**
     * Returns the filename portion of a file specification string.
     * @return The filename string with extension.
     */
    public static String filename(String filename) {
        int i = filename.lastIndexOf(File.separator);
        return (i >= 0) ? filename.substring(i+1) : filename;
    }



    /**
     * Returns the filename portion of a file specification string.
     * Matches the equally named unix command.
     * @return The filename string without extension.
     */
    public static String basename(String filename) {
        return basename(filename, extension(filename));
    }

    /**
     * Returns the filename portion of a file specification string.
     * Matches the equally named unix command.
     */
    public static String basename(String filename, String suffix) {
        int i = filename.lastIndexOf(File.separator);
        int lastDot = ((suffix != null) && (suffix.length() > 0))
                ? filename.lastIndexOf(suffix) : -1;

        if (lastDot >= 0) {
            return filename.substring(i, lastDot);
        } else if (i > 0) {
            return filename.substring(i);
        } else {
            return filename; // else returns all (no path and no extension)
        }
    }

    /**
     * Returns the extension portion of a file specification string.
     * This everything after the last dot '.' in the filename (including
     * the dot).
     */
    public static String extension(String filename) {
        int lastDot = filename.lastIndexOf('.');

        if (lastDot >= 0) {
            return filename.substring(lastDot);
        } else {
            return "";
        }
    }

    /**
     * Check if a file exits.
     *
     * @param fileName The name of the file to check.
     * @return true if file exists.
     */
    public static boolean fileExists(String fileName) {
        File file = new File(fileName);
        return file.exists();
    }

    /**
     * Reads the contents of a file.
     *
     * @param fileName The name of the file to read.
     * @return The file contents or null if read failed.
     */
    public static String fileRead(String fileName) throws Exception {
        StringBuffer buf = new StringBuffer();

        FileInputStream in = new FileInputStream(fileName);

        int count;
        byte[] b = new byte[512];
        while ((count = in.read(b)) > 0) // blocking read
        {
            buf.append(new String(b, 0, count));
        }

        in.close();

        return buf.toString();
    }

    /**
     * Writes data to a file. The file will be created if it does not exist.
     *
     * @param fileName The name of the file to write.
     * @param data The content to write to the file.
     */
    public static void fileWrite(String fileName, String data) throws Exception {
        FileOutputStream out = new FileOutputStream(fileName);
        out.write(data.getBytes());
        out.close();
    }

    /**
     * Deletes a file.
     *
     * @param fileName The name of the file to delete.
     */
    public static void fileDelete(String fileName) {
        File file = new File(fileName);
        file.delete();
    }

    /**
     * Waits for NFS to propagate a file creation, imposing a timeout.
     *
     * @param fileName The name of the file.
     * @param seconds The maximum time in seconds to wait.
     * @return True if file exists.
     */
    public static boolean waitFor(String fileName, int seconds) {
        File file = new File(fileName);
        int timeout = 0;
        int tick = 0;
        while (!file.exists()) {
            if (tick >= 10) {
                tick = 0;
                if (timeout > seconds) {
                    return false;
                }
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException ignore) {
            } catch (Exception ex) {
                break;
            }
        }
        return true;
    }

    /**
     * Creates a file handle.
     *
     * @param fileName The name of the file.
     * @return A <code>File</code> instance.
     */
    public static File getFile(String fileName) {
        return new File(fileName);
    }
}
